(function($){
    $.fn.extend({
        /*op = {
         * onOutof:function(){}超过限制数量处理
         * multiple:true 多图上传，默认单图
         * fileName:"name" 上传文件name
         * initFileName:"initName"
         * initData:[]初始化
         * standard:true;//是否标准模式上传 (file[0],file[1])
         * clickView:function(){
         * }
         * style:按钮样式
         * readonly:flase;
         * }*/
        treeTable:function(op){
            var tTable = {};
            var trGroup = this;
            tTable.model = buildTree(op.data,op.buildTree);
            trGroup.data("treeTable",tTable);

            paintRoot();

            function paintRoot(){
                trGroup.empty();
                for (var i = 0; i < tTable.model.length; i++) {
                    var mo = tTable.model[i];
                    trGroup.append(initTr(mo,0,true));
                }
            }

            function initTr(model,level,isclose){
                var bt = op.buildTree;
                var $tr = $(op.getTr(model,level,isclose));
                $tr.attr("data-pid",model[bt.pidKey]);
                $tr.attr("data-id",model[bt.idKey]);
                $tr.attr("data-isclose",isclose?"1":"");
                $tr.attr("data-level",level);
                $tr.data("treeModel",model);
                return $tr;
            }

            tTable.switchItem = function(id){
                var $tr = trGroup.find(">tr[data-id="+id+"]");
                if($tr.attr("data-isclose")){
                    var $ctr = initTr($tr.data("treeModel"),$tr.attr("data-level"),false);
                    $tr.replaceWith($ctr);
                    printChild($ctr);
                }else{
                    var $ctr = initTr($tr.data("treeModel"),$tr.attr("data-level"),true);
                    $tr.replaceWith($ctr);
                    removeChild($ctr);
                }
            }

            function printChild(tr){
                var childs = tr.data("treeModel").childs;
                if(!childs||childs.length<1)return;
                var level = new Number(tr.attr("data-level"))+1;
                for (var i = 0; i < childs.length; i++) {
                    var ctr = initTr(childs[i],level,true);
                    $(tr).after(ctr);
                }
            }

            function removeChild(tr) {
                var id = tr.attr("data-id");
                var ch = trGroup.find(">tr[data-pid="+id+"]");
                ch.each(function(i,e){
                    removeChild($(e));
                });
                ch.remove();
            }

            /**
             *
             * @param array
             * @param op {idKey:"id"//对应数据id的key
                    ,pidKey:"parentid"//对应数据父id的key
                    ,rootVal:0
                    ,childKey:'childs'//构建子节点属性名}
             * @returns {Array}
             */
            function buildTree(array,op){
                if(!op)op = {};

                var pidname = !op.pidKey?"pid":op.pidKey;
                var childname = op.childKey?op.childKey:"childs";
                var idname = op.idKey?op.idKey:"id";
                var rootVal = op.rootVal?op.rootVal:0;

                var root = [];
                for (var i = 0; i < array.length; i++) {
                    if(rootVal==array[i][pidname])
                        root.push(array[i]);


                    var pid = array[i][idname];

                    array[i].childs = getbyPid(array,pid);
                }



                function getbyPid(array,pid){

                    var l = [];
                    for (var i = 0; i < array.length; i++) {
                        if(array[i][pidname]==pid)
                            l.push(array[i]);
                    }

                    return l;
                }

                return root;

            }

            return tTable;


        }
    })
})(jQuery);